package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.smarttraffic.InitApp;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.LitepalBean.DBUserCollectBean;
import com.lenovo.smarttraffic.bean.LitepalBean.DBUserInfoBean;
import com.lenovo.smarttraffic.ui.activity.EnvironmentActivity;
import com.lenovo.smarttraffic.ui.activity.UserCollectActivity;
import com.lenovo.smarttraffic.util.CommonUtil;

import org.litepal.LitePal;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.text.CollationElementIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.logging.Handler;

/**
 * Created by Caleb.F
 * on 2020-03-17
 * at 18:31
 */
public class MyUserCollectRecyclerAdapter extends RecyclerView.Adapter<MyUserCollectRecyclerAdapter.MyCollectViewHolder> {
    private static final String TAG = "MyUserCollectRecyclerAd";
    private Context mContext;
    private List<DBUserCollectBean> mlist;

    public MyUserCollectRecyclerAdapter(Context context, List<DBUserCollectBean> list) {
        mContext = context;
        mlist=list;
        sortList();
    }

    @NonNull
    @Override
    public MyCollectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyCollectViewHolder(LayoutInflater.from(mContext).inflate(R.layout.userinfo_datail_collect
                , parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyCollectViewHolder holder, int position) {
        setData(holder, position);
        if (mlist.get(position).getTopTimemills()!=0) {
            holder.mTvFirst.setText("取消置顶");
            holder.mTvSecond.setText("取消收藏");
            holder.itemView.setBackgroundColor(Color.parseColor("#B3B0B0"));
            holder.mTvFirst.setOnClickListener(v -> {
                //取消置顶：
                DBUserCollectBean dbUserCollectBean = mlist.get(position);
                dbUserCollectBean.setTopTimemills(0);
                sortList();
                notifyDataSetChanged();
                //更新数据库
                updateDBdata(0, dbUserCollectBean.getUserName());
                ((ConstraintLayout) v.getParent().getParent()).scrollTo(0, 0);
            });
            holder.mTvSecond.setOnClickListener(v -> {
                DBUserCollectBean remove = mlist.remove(position);
                LitePal.deleteAll(DBUserCollectBean.class,
                        "userName = ?", remove.getUserName());
                notifyItemRemoved(position);
            });
        } else {
            holder.mTvFirst.setText("取消收藏");
            holder.mTvSecond.setText("置顶");
            holder.itemView.setBackgroundColor(Color.WHITE);
            holder.mTvFirst.setOnClickListener(v -> {
                DBUserCollectBean remove = mlist.remove(position);
                LitePal.deleteAllAsync(DBUserCollectBean.class, "userName = ?", remove.getUserName());
                notifyItemRemoved(position);
            });
            holder.mTvSecond.setOnClickListener(v -> {
                DBUserCollectBean dbUserCollectBean = mlist.get(position);
                Log.d(TAG, "onBindViewHolder: "+System.currentTimeMillis());
                dbUserCollectBean.setTopTimemills(System.currentTimeMillis());
                updateDBdata(System.currentTimeMillis(), dbUserCollectBean.getUserName());
                sortList();
                notifyDataSetChanged();
            });

        }


    }


    private void updateDBdata(long topTimemils, String userName) {
        DBUserCollectBean dbUserCollectBean1 = new DBUserCollectBean();
        dbUserCollectBean1.setTopTimemills(topTimemils);
        dbUserCollectBean1.updateAll("userName=?", userName);
    }

    private void setData(@NonNull MyCollectViewHolder holder, int position) {
        if (mlist.get(position).getUserSex().equals("男")) {
            holder.mImageView.setImageResource(R.mipmap.touxiang_2);
        } else {
            holder.mImageView.setImageResource(R.mipmap.touxiang_1);
        }
        holder.mTvUsername.setText("用户名: " + mlist.get(position).getUserName());
        holder.mTvPname.setText("姓名: " + mlist.get(position).getUserPname());
        holder.mTvTelephone.setText("电话: " + mlist.get(position).getUserTel());
        holder.mTvTime.setText(mlist.get(position).getAddTime());
    }

    private  void sortList(){
        Collections.sort(mlist, new Comparator<DBUserCollectBean>() {
            @Override
            public int compare(DBUserCollectBean o1, DBUserCollectBean o2) {
                if(o1.getTopTimemills() == o2.getTopTimemills())
                {
                    return -Long.compare(o1.getCollectTime(), o2.getCollectTime());
                }
                return -Long.compare(o1.getTopTimemills(), o2.getTopTimemills());
            }
        });
    }
    @Override
    public int getItemCount() {
        return mlist.size();
    }


    class MyCollectViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;
        TextView mTvUsername;
        TextView mTvPname;
        TextView mTvTelephone;
        TextView mTvDetail;
        TextView mTvFirst;
        TextView mTvSecond;
        TextView mTvTime;

        public MyCollectViewHolder(View convertView) {
            super(convertView);
            mImageView = convertView.findViewById(R.id.imageView);
            mTvUsername = convertView.findViewById(R.id.tv_username);
            mTvPname = convertView.findViewById(R.id.tv_pname);
            mTvTelephone = convertView.findViewById(R.id.tv_telephone);
            mTvDetail = convertView.findViewById(R.id.tv_detail);
            mTvFirst = convertView.findViewById(R.id.tv_first);
            mTvSecond = convertView.findViewById(R.id.tv_second);
            mTvTime = convertView.findViewById(R.id.tv_time);

        }

    }
}
